package leetcode_1208

func equalSubstring(s string, t string, maxCost int) int {
	result := 0

	length := len(s)

	// 转换为数组
	tmpArr := []int{}
	for i := 0; i < length; i++ {
		tmp := 0
		if t[i] > s[i] {
			tmp = int(t[i] - s[i])
		} else {
			tmp = int(s[i] - t[i])
		}

		tmpArr = append(tmpArr, tmp)
	}

	// 求数组不大于maxCost的最大长度
	left, right, sum := 0, 0, 0

	for right < length {
		sum += tmpArr[right]

		for sum > maxCost {
			result = max(result, right-left)
			sum -= tmpArr[left]
			left++
		}

		result = max(result, right-left+1)
		right++
	}

	return result
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}
